﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Npgsql;
using Ortodoctor.Componentes;

namespace Ortodoctor.modelo
{
    class ModeloClinica
    {
        //conexao con = new conexao();
        //string connectionstring = ("Server=186.202.152.24;Port=5432;User Id=ortodoctor1;Password=DycbqDfHLI;Database=ortodoctor1;MinPoolSize=5;CommandTimeout=60;");
        string connectionstring = ("Server=localhost;Port=5432;User Id=postgres;Password=postgres;Database=ortodoctor;MinPoolSize=5;CommandTimeout=60;"); NpgsqlCommand com;
        NpgsqlConnection conn;
        NpgsqlDataAdapter da;
        public ModeloClinica()
        {
            conn = new NpgsqlConnection(connectionstring);
            conn.Open();
        }

        public int insert(Clinica c)
        {
            object obj;
            try
            {
                //con.open();
                String sql = "INSERT INTO clinica(id_endereco) " +
                             "VALUES (:p1)";
                NpgsqlCommand com = new NpgsqlCommand(sql, conn);
                com.Parameters.AddWithValue("p1", c.Endereco.IdEndereco);
                obj = com.ExecuteScalar();
            }
            finally
            {
                conn.Close();
            }
            return obj != null ? (int.Parse(obj.ToString())) : getMaxId();
        }

        private int getMaxId()
        {
            DataTable dt = new DataTable();
            try
            {
                //con.open();
                String sql = "SELECT Max(id_clinica) as id_clinica FROM clinica";
                NpgsqlDataAdapter da = new NpgsqlDataAdapter(sql, conn);
                da.Fill(dt);
            }
            finally
            {
                conn.Close();
            }
            return Convert.ToInt32(dt.Rows[0]["id_clinica"]);
        }

        public DataTable getAll()
        {
            DataTable dt = new DataTable();
            try
            {
                //con.open();
                String sql = "SELECT * FROM clinica INNER JOIN endereco USING(id_endereco)";
                NpgsqlDataAdapter da = new NpgsqlDataAdapter(sql, conn);
                da.Fill(dt);
            }
            finally { conn.Close(); }
            return dt;
        }

        public DataTable getClinica(int id)
        {
            DataTable dt = new DataTable();
            try
            {
                //con.open();
                String sql = "SELECT * FROM clinica INNER JOIN endereco USING(id_endereco) " +
                             "WHERE id_clinica=:id";
                NpgsqlCommand com = new NpgsqlCommand(sql, conn);
                com.Parameters.AddWithValue("id", id);
                NpgsqlDataAdapter da = new NpgsqlDataAdapter(com);
                da.Fill(dt);
            }
            finally { conn.Close(); }
            return dt;
        }

        public void update(Clinica c)
        {
            try
            {
                //con.open();
                String sql = "UPDATE clinica SET id_endereco=:ide " +
                             "WHERE id_clinica=:id";
                NpgsqlCommand com = new NpgsqlCommand(sql, conn);
                com.Parameters.AddWithValue("ide", c.Endereco.IdEndereco);
                com.Parameters.AddWithValue("id", c.IdClinica);
                com.ExecuteNonQuery();
            }
            finally { conn.Close(); }
        }

        public void delete(int id)
        {
            try
            {
                //con.open();
                String sql = "DELETE FROM clinica WHERE id_clinica=:id";
                NpgsqlCommand com = new NpgsqlCommand(sql, conn);
                com.Parameters.AddWithValue("id", id);
                com.ExecuteNonQuery();
            }
            finally
            {
                conn.Close();
            }
        }
    }
}
